package com.wuzheng.wisgo.sanitation.cloud.service.gis.impl;

import com.alibaba.fastjson.JSONObject;
import com.wuzheng.wisgo.sanitation.cloud.paraandgetutil.HttpClientUtil;
import com.wuzheng.wisgo.sanitation.cloud.repository.entity.gis.MapBase;
import com.wuzheng.wisgo.sanitation.cloud.repository.mapper.gis.MapBaseMapper;
import com.wuzheng.wisgo.sanitation.cloud.service.gis.MapBaseService;
import com.wuzheng.wisgo.sanitation.framework.base.service.impl.BaseServiceImpl;
import com.wuzheng.wisgo.sanitation.framework.util.GPSConverterUtils;
import com.wuzheng.wisgo.sanitation.framework.util.model.GPS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @author：sp
 * @date：Created in 2019-10-16
 * @description：经纬度转换服务实现类
 * @modified By：
 * @version: 1.0
 */
@Service
public class MapBaseServiceImpl extends BaseServiceImpl<MapBase> implements MapBaseService {

    @Autowired
    private MapBaseMapper mapBaseMapper;

    @Override
    public int selectLonLat(Double lon, Double lat, String mapType) {
        return mapBaseMapper.selectByVehId(lon,lat,mapType);
    }

    public MapBase getAddressByPoint(String url_getAdByPoint, String amap_key, Double lat,Double log,String mapType) throws Exception{
        //用于封装转换后的经纬度参数跟实际地址
        MapBase mapBase=new MapBase();
        mapBase.setId(UUID.randomUUID().toString());
        mapBase.settLongitude(log);
        mapBase.settLatitude(lat);
        mapBase.settMapType(mapType);
        mapBase.setDeFlag(false);
        mapBase.setCreator("sp");
        mapBase.setCreateTime(new Date());
        mapBase.setModifier("sp");
        mapBase.setModifyTime(new Date());
        mapBase.setIpAddress("127.0.0.1");
        //转换坐标为高德GPS
        long startGPS = System.currentTimeMillis();
        GPS gps = GPSConverterUtils.gps84ToGcj02(lat, log);
        logger.info("高德GPS数据转换时间："+(System.currentTimeMillis()-startGPS));
        //封装转换后的经纬度
        mapBase.settTranLatitude(gps.getLat());
        mapBase.settTranLongitude(gps.getLon());
        //http连接
        String url = url_getAdByPoint.replace("#{lat}", String.valueOf(gps.getLat())).replace("#{log}", String.valueOf(gps.getLon())).replace("#{key}", amap_key);
        String jsonString="";
        long startAddr = System.currentTimeMillis();
        jsonString = HttpClientUtil.getString(url);
        logger.info("高德GPS地址转换时间："+(System.currentTimeMillis()-startAddr));
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        //转换后的地址
        mapBase.settAddress(jsonObject.getJSONObject("regeocode").getString("formatted_address"));
        return mapBase;
    }
}
